import {reactive, ref} from "vue";
import http from "../http";
import doctor from "./doctor.js";
import {ElMessage} from "element-plus";
import patient from "./patient.js";
import router from "../router/index.js";
const pay = reactive({
    imageUrlList: [],
    imageList: [],
    question: "",
    doctor_id: "",
    user_coupon_id: 0,
    discount_name: "通用类型",
    discount: 0,
    coupon_name: "",
    price: 0,
    is_public: false,

    pay_time: undefined,     // 付款时间
    real_price: 0,       // 勾选商品列表的实付总价格
    loading: false,           // 显示订单支付的倒计时的遮罩层
    timeout: 15*60,           // 订单倒计时
    timer: null,              // 订单支付的定时器标志
    order_id: null,           // 当前订单ID

    order_status: -1,        // 个人中心的默认显示的订单状态选项，-1表示全部
    order_status_chioces:[], // 个人中心的订单支付状态选项
    page: 1,                 // 个人中心的订单列表对应的页码
    size: 5,                 // 个人中心的订单列表对应的单页数据量
    order_list:[],           // 个人中心的订单列表
    count: 0,                // 个人中心的订单列表的总数据量

    get_doctor_info(){
        this.doctor_id = localStorage.getItem("doctor_id")
        return http.get(`/doctor/doctor_info/${this.doctor_id}/`).then(response=>{
            doctor.doctor_info = response.data;
        })
    },
    create_order(token){
        // 创建订单
        let data = {
            pay_type: 0,
            doctor_id: localStorage.getItem("doctor_id"),
            user_coupon_id: this.user_coupon_id,
            discount_name: this.discount_name,
            is_public: this.is_public,
            patients_id: patient.patient_id
        }
        console.log("订单")
        console.log(data)
        return http.post("order/orders/",data,{
            headers:{
                Authorization: "Bearer " + token,
            }
        }).then(response=>{
            ElMessage.success("下单成功！马上跳转到支付页面，请稍候~");
            this.order_id = response.data.id;
            this.timeout = response.data.timeout;
            this.get_pay_link(this.order_id);
            this.loading = true; // 显示支付倒计时
            clearInterval(this.timer);
            this.timer = setInterval(()=>{
                if(order.timeout > 1) {
                    order.timeout = order.timeout - 1;
                }else{
                    ElMessage.error("订单超时！如果您已经支付成功！请点击关闭当前弹窗！当前页面1.5秒后关闭！");
                    clearInterval(this.timer);
                    // 发送一个订单查询
                    this.check_order_result().then(response=>{
                        console.log(response);
                        if(response.status === 201){
                            // 支付完成！
                            ElMessage.success("支付成功！自动跳转至首页页面，请稍候~");
                            this.get_alipay_return_result()
                            // 1.5秒后关闭页面，跳转到用户中心的我的订单页面中
                            setTimeout(()=>{
                                // 跳转到用户中心的课程列表
                                router.push("/order");
                            }, 1500);
                        }else{
                            ElMessage.info("您的订单尚未支付！马上跳转到个人中心页面，请稍候~");
                        }
                    })
                    // 1.5秒后关闭页面，跳转到用户中心的我的订单页面中
                    setTimeout(()=>{
                        // 跳转到用户的订单用心
                        router.push("/order");
                    }, 1500);
                }
            }, 1000);

        }).catch((error)=>{
            console.log('创建订单失败......');
            console.log(error);
            if (error.response.data[0] === "问题已经被提交过") {
                ElMessage.error("问题已经被提交过，无需重新提交，正在跳转我的问诊界面！")
                setTimeout(()=>{
                    // 跳转到用户的订单用心
                    router.replace("/order");
                }, 1500);
            }
        })
    },
    get_pay_link(order_id){
        // 获取支付宝支付链接地址
        return http.get("/pay/alipay/link/", {
            params: {
                order_id,
            }
        }).then(response=>{
            // window.open(response.data,"_blank");
            window.location.href = response.data
        })
    },
    get_alipay_return_result(){
        // 客户端转发支付宝的同步支付结果
        return http.get(`/pay/alipay/return_result/${location.search}`).then(response=>{
            console.log('支付成功返回结果...');
            console.log(response);
            this.pay_time = response.data.pay_time;
            this.real_price = response.data.real_price;
        })
    },
    check_order_result(){
        // 查询订单的支付状态
        console.log(`查询订单ID=${this.order_id}的支付结果！`);
        return http.get("/payments/alipay/query/", {
            params: {
                order_id: this.order_id,
            },
        })
    },
    get_order(token){
        return http.get("/order/order_list/", {
            headers:{
                Authorization: "Bearer " + token,
            }
        })
        .then(response=>{
            console.log(response.data.data)
            this.order_list = response.data.data
        })
    }

})

export default pay;